// 域名
const HOST = "https://diancan.schlhjnetworktesturl.com/wechat/v1";

// 配置文件
const SHOP_CONFIG_FILE = require('../../utils/shop_config.js').SHOP_CONFIG;

// 微信扩展方法
const $wx = require('$wx_extend.js');
let showModal;

// 请求对象类
class $request {
  constructor(host, config){
    this.host = host; 
    this.config = config;
    this.wxRequet = wx.request;
    
    // 检查请求前状态
    this.testRequestBeforeStatus = (fun)=>{
      wx.getNetworkType({
        success: netWorkRes => {
          if (netWorkRes == 'none') {
            $wx.showToast('~ 好像没网了 ~', callBack => {
              wx.onNetworkStatusChange(netWorkChangeRes => {
                if (netWorkChangeRes.networkType != 'none') {
                  fun();
                }
              });
            });
          }else{
            fun();
          }
        }
      })
    }

    // 获取函数所有形参
    this.getFunFormalparameter = (funName) =>{
      let regExp = /function\s*\w*\(([\s\S]*?)\)/;
      if (regExp.test(funName)) {
        return RegExp.$1.split(',')
      }
    }

    // 检测是否是函数
    this.isFun = (funName)=>{
      if (typeof funName === 'function'){
        return true
      }else{
        return false
      }
    }
  }
  
  GET = (url, data = {}, success, fail)=>{
    let config = this.config, params = {};  

    this.testRequestBeforeStatus(callBack =>{
      if (data.header)
        params.header = data.header && delete data.header;
      else if (config.header)
        params.header = this.isFun(config.header) ? config.header(url) : config.header;

      params.url = this.host + url;
      params.data = data;
      params.success = res => config.success(res, success);
      // params.fail = fail;   
      params.fail = error => config.fail(error, fail);
      this.wxRequet(params);
      
    });
  }

  POST = (url, data = {} , success, fail) =>{
    let config = this.config, params = {};  
    
    this.testRequestBeforeStatus(callBack => {
      if (data.header) 
        params.header = data.header && delete data.header; 
      else if(config.header)
        params.header = this.isFun(config.header) ? config.header(url) : config.header;
        

      params.method = 'POST';
      params.url = this.host + url;
      params.data = data;      
      params.success = res => config.success(res, success);
      // params.fail = fail;
      params.fail = error => config.fail(error, fail);
      this.wxRequet(params);
    });
  }
}

const $REQUEST = new $request(HOST, {
  header(url){
    let filter_regExp = url.replace(/\/\w+\//, '');
    let filter_url = {};
    
    if (!filter_url[filter_regExp]){
      return {
        token: wx.getStorageSync('token'),
        id: wx.getStorageSync('shopConfig').shop_id,
        key: wx.getStorageSync('sceneInfo').key
      }
    }
  },
  
  success(res, callBack){
    let rd = res.data;
    if (rd.code == 201){
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      if (!showModal || sohwModal == null){
        showModal = wx.showModal;
        showModal({
          title: '提示',
          content: '请先登录',
          success(res) {
            if (res.confirm) {
              showModal = null;
              // wx.clearStorage({
              //   success() {
              //     wx.reLaunch({
              //       url: '/pages/other-pages/authorization-login/authorization-login'
              //     });
              //   }
              // });

              wx.navigateTo({
                url: '/pages/other-pages/authorization-login/authorization-login'
              })
            }
          }
        });
      }
    } else if (rd.code == 1){
      callBack(true, rd.data, rd.msg);
    }else{
      callBack(false, rd, rd.msg);
    }
  },

  fail(error, callBack){
    wx.hideLoading();
    wx.hideNavigationBarLoading();
    if (error.errMsg == 'request:fail timeout'){
      $wx.showToast('请求超时');
    }
    typeof callBack === 'function' && callBack(error);
  }
}),
GET = $REQUEST.GET,
POST = $REQUEST.POST;



// 判断是否扫码
export const isScanCode = (success, fail) => {
  return GET('/Shop/isTrue', {}, success, fail);
}

// 登录
export const login = (params,success,fail)=>{
  return POST('/user/userLogin', params, success, fail);
}

// 商店配置
export const getShopConfig = (params, success, fail) => {
  return GET('/Shop/getShopConfig', params, success, fail);
}

// 列表
export const getGoodsList = (params, success, fail) => {
  return POST('/Goods/getGoodsList', params, success, fail);
}

// 获取商品规格
export const getCommoditySpce = (params, success, fail) => {
  return POST('/Goods/getGoodsAttr', params, success, fail);
}

// 修改购物车
export const editCar = (params, success, fail) => {
  return POST('/Car/editCar', params, success, fail);
}

// 购物车列表
export const getCarList = (params, success, fail) => {
  return GET('/Car/getCarList', params, success, fail);
}

// 删除购物车
export const deleteCar = (params, success, fail) => {
  return POST('/Car/deleteCar', params, success, fail);
}

// 获取配送时间
export const getDeliveryTime = (params, success, fail) => {
  return GET('/Order/getDeliveryTime', params, success, fail);
}

// 获取用户信息
export const getUserInfo = (params, success, fail) => {
  return GET('/user/userInfo', params, success, fail);
}

// 获取手机验证码
export const getVerificationCode = (params, success, fail) => {
  return POST('/user/getMobileCode', params, success, fail);
}

// 修改手机
export const updatePhone = (params, success, fail) => {
  return POST('/user/updateMobile', params, success, fail);
}

// 修改昵称
export const updateNickName = (params, success, fail) => {
  return POST('/user/updateNick', params, success, fail);
}

// 上传图片
export const uploadImg = HOST + '/Commonality/uploadImg';

// 修改头像
export const updateAvatr = (params, success, fail) => {
  return POST('/user/updateHead', params, success, fail);
}

// 获取地址列表
export const getAddList = (params, success, fail) => {
  return GET('/userAddress/getAddList', params, success, fail);
}

// 添加地址
export const addAddress = (params, success, fail) => {
  return POST('/userAddress/insertAdd', params, success, fail);
}

// 添加地址
export const deleteAddress = (params, success, fail) => {
  return POST('/userAddress/deleteAdd', params, success, fail);
}

// 添加地址
export const addressDetails = (params, success, fail) => {
  return GET('/userAddress/getDetailById', params, success, fail);
}

// 修改地址
export const updateAddress = (params, success, fail) => {
  return POST('/userAddress/updateAddress', params, success, fail);
}

// 商品详情
export const getGoodsDetail = (params, success, fail) => {
  return POST('/Goods/getGoodsDetail', params, success, fail);
}

// 商品点赞
export const like = (params, success, fail) => {
  return POST('/Goods/likeGoodsById', params, success, fail);
}

// 设置默认
export const setDefaultAddress = (params, success, fail) => {
  return POST('/userAddress/setDefault', params, success, fail);
}

// 结算页面(去结算（创建订单）)
export const createOrder = (params, success, fail) => {
  return GET('/Order/createOrder', params, success, fail);
}

// 生成订单
export const addOrder = (params, success, fail) => {
  return POST('/Order/addOrder', params, success, fail);
}

// 订单支付
export const payOrder = (params, success, fail) => {
  return POST('/PayMoney/payOrder', params, success, fail);
}

// 再来一单
export const anotherOrder = (params, success, fail) => {
  return POST('/Order/anotherOrder', params, success, fail);
}

// 订单列表
export const getOrderList = (params, success, fail) => {
  return GET('/Order/getOrderList', params, success, fail);
}

// 订单详情
export const getOrderDetail = (params, success, fail) => {
  return GET('/Order/getOrderById', params, success, fail);
}

// 评论
export const commen = (params, success, fail) => {
  return POST('/Order/commen', params, success, fail);
}

// 评论列表
export const commentList = (params, success, fail) => {
  return POST('/shop/commentList', params, success, fail);
}

// 评论列表
export const getShopInfo = (params, success, fail) => {
  return GET('/Shop/getShop', params, success, fail);
}

// 充值
export const payRecharge = (params, success, fail) => {
  return POST('/PayMoney/payRecharge', params, success, fail);
}

// 删除订单
export const delOrder = (params, success, fail) => {
  return POST('/order/delOrder', params, success, fail);
}

// 用户余额
export const getMyMoney = (params, success, fail) => {
  return GET('/user/getMyMooney', params, success, fail);
}

// 退单
export const cancelOrder = (params, success, fail) => {
  return POST('/Order/exitOrderById', params, success, fail);
}

// 增加扫码记录
export const addScanKey = (params, success, fail) => {
  return POST('/Index/userKey', params, success, fail);
}

// 查看扫码记录
export const getScanKey = (params, success, fail) => {
  return POST('/Index/getUserKey', params, success, fail);
}




